####################################################################
#***2019 analysis
## Packages
# To install and open the R packages that you need for this code. 
need <- c('tidyverse','readstata13','lfe','glue','rdrobust', 'stargazer','arm', 'broom', 'ggplot2', 'dotwhisker', 'gridExtra', 'ggeffects')
have <- need %in% rownames(installed.packages()) 
if(any(!have)) install.packages(need[!have]) 
invisible(lapply(need, library, character.only=T)) 

# Change path to whereever you place the models
# To set up the working directory. 
script_folder = dirname(rstudioapi::getSourceEditorContext()$path)
setwd(glue('{script_folder}'))
rm(list = ls())
setwd("../")

## load in RD data:
load("5prepdata/final_rd_data.RData")

rd.data$mds1_std<-(rd.data$mds1-mean(rd.data$mds1, na.rm=TRUE))/sd(rd.data$mds1, na.rm=TRUE)
summary(rd.data$mds1_std)
rd.data$wonXMDSstd <- rd.data$won_election*rd.data$mds1_std

main.specification <- function(x) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  
  out <- felm(x[rd.data$kw>0] ~ won_election + wonXMDSstd + mds1_std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  #out <- lm(x[rd.data$kw>0] ~ won_election + won_election + wonXMDSstd + mds1_std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates + as.factor(year) + const, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  bw <- round(rd.data$bandwidth[1], 2)
  
  return(list(out, bw))
}

m1<-main.specification(rd.data$ever_ranprim_h)
m2<-main.specification(rd.data$ever_winprim_h)
m3<-main.specification(rd.data$ever_runhouse)
m4<-main.specification(rd.data$ever_winhouse)



stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service interacted with state legislative professionalism",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Table 4.tex",
          label = "tab:interactions",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c("won_election", "mds1_std", "wonXMDSstd"), order = c(1, 3, 2),
          covariate.labels = c("Won", "Professionalism", "Ever Won x Professionalism"), 
          add.lines = list(c("Bandwidth", m1[[2]], m2[[2]], m3[[2]], m4[[2]])),
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with a globally unit-standardized professionalism score for the state-chamber-year based on \\citet{bowen2014}. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)


###################
# OTHER INTERACTIONS
###################

rd.data$salary_real<-(rd.data$salary_real-mean(rd.data$salary_real, na.rm=TRUE))/sd(rd.data$salary_real, na.rm=TRUE)
  rd.data$wonXsalary <- rd.data$won_election*rd.data$salary_real

rd.data$slength<-(rd.data$slength-mean(rd.data$slength, na.rm=TRUE))/sd(rd.data$slength, na.rm=TRUE)
  rd.data$wonXlength <- rd.data$won_election*rd.data$slength
  
rd.data$expend<-(rd.data$expend-mean(rd.data$expend, na.rm=TRUE))/sd(rd.data$expend, na.rm=TRUE)
  rd.data$wonXexpend <- rd.data$won_election*rd.data$expend
  
main.specification.addl <- function(x) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  out <- felm(x[rd.data$kw>0] ~ won_election + wonXsalary + wonXlength+ wonXexpend + salary_real + slength + expend + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  rd.data$mainefx<-NULL
  rd.data$interaction<-NULL
  bw <- round(rd.data$bandwidth[1], 2)
  
  return(list(out, bw))
}

  
  
  
m1<-main.specification.addl(rd.data$ever_ranprim_h)
m2<-main.specification.addl(rd.data$ever_winprim_h)
m3<-main.specification.addl(rd.data$ever_runhouse)
m4<-main.specification.addl(rd.data$ever_winhouse)

stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service interacted with separate professionalism components",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 12.tex",
          label = "tab:interactions2",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c('won_election','wonXsalary' ,'wonXlength', 'wonXexpend'),
          add.lines = list(c("Bandwidth", m1[[2]], m2[[2]], m3[[2]], m4[[2]])),
          covariate.labels = c("Won","Won * salary", "Won * Sess. length", "Won * Expend."), 
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with a globally unit-standardized professionalism score for the state-chamber-year based on \\citet{bowen2014}. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)



class(rd.data$rev.cat)
rd.data$rev.cat_num[rd.data$rev.cat=="none"]<-0
rd.data$rev.cat_num[rd.data$rev.cat=="low"]<-1
rd.data$rev.cat_num[rd.data$rev.cat=="medium"]<-2
rd.data$rev.cat_num[rd.data$rev.cat=="high"]<-3
table(rd.data$rev.cat_num)


rd.data$wonXrev <- rd.data$won_election*rd.data$rev.cat_num

main.specification.addl <- function(x) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year + rd.data$wonXrev + rd.data$rev.cat_num)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  out <- felm(x[rd.data$kw>0] ~ won_election + wonXrev + rev.cat_num + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  rd.data$mainefx<-NULL
  rd.data$interaction<-NULL
  bw <- round(rd.data$bandwidth[1], 2)
  
  return(list(out, bw))
}


## models:

main.specification.revprof <- function(x, cat) {
  rd.data <- filter(rd.data, prof.rank.cat %in% cat)
  
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year + rd.data$wonXrev + rd.data$rev.cat_num)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  out <- felm(x[rd.data$kw>0] ~ won_election + wonXrev + rev.cat_num + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  rd.data$mainefx<-NULL
  rd.data$interaction<-NULL
  return(out)
}

m1 <- main.specification.revprof(rd.data$ever_runhouse[rd.data$prof.rank.cat=="low" | rd.data$prof.rank.cat == "med"], c("low", "med"))
m2 <- main.specification.revprof(rd.data$ever_runhouse[rd.data$prof.rank.cat=="high"], "high")



lowprof <- filter(rd.data, prof.rank < 25)

lowprof$cutoff<-NULL
lowprof$bandwidth<-NULL
lowprof$kw<-NULL

y<-rdbwselect(lowprof$ever_runhouse, lowprof$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = lowprof$running_terms_served + lowprof$dem + lowprof$rep + lowprof$spc_elec + lowprof$term_length + lowprof$number_candidates + lowprof$year + lowprof$wonXrev + lowprof$rev.cat_num)
lowprof$bandwidth<-y$bws[1]
rm(y)

# gen cutoff = 0
lowprof$cutoff <- 0
# gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
lowprof$kw <- 1-(abs(lowprof$cutoff-lowprof$victory_marg))/lowprof$bandwidth
# replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth)
lowprof$kw[lowprof$victory_marg>(lowprof$cutoff+lowprof$bandwidth) | lowprof$victory_marg<(lowprof$cutoff-lowprof$bandwidth)] <- 0

out1 <- felm(lowprof$ever_runhouse[lowprof$kw>0] ~ won_election + wonXrev + rev.cat_num + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = lowprof[lowprof$kw>0,], weights = lowprof$kw[lowprof$kw>0])

bw <- round(lowprof$bandwidth[1], 2)

out1 <- list(out1, bw)


highprof <- filter(rd.data, prof.rank > 24)

highprof$cutoff<-NULL
highprof$bandwidth<-NULL
highprof$kw<-NULL

y<-rdbwselect(highprof$ever_runhouse, highprof$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = highprof$running_terms_served + highprof$dem + highprof$rep + highprof$spc_elec + highprof$term_length + highprof$number_candidates + highprof$year + highprof$wonXrev + highprof$rev.cat_num)
highprof$bandwidth<-y$bws[1]
rm(y)

# gen cutoff = 0
highprof$cutoff <- 0
# gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
highprof$kw <- 1-(abs(highprof$cutoff-highprof$victory_marg))/highprof$bandwidth
# replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth)
highprof$kw[highprof$victory_marg>(highprof$cutoff+highprof$bandwidth) | highprof$victory_marg<(highprof$cutoff-highprof$bandwidth)] <- 0

out2 <- felm(highprof$ever_runhouse[highprof$kw>0] ~ won_election + wonXrev + rev.cat_num + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = highprof[highprof$kw>0,], weights = highprof$kw[highprof$kw>0])

bw <- round(highprof$bandwidth[1], 2)

out2 <- list(out2, bw)



stargazer(list(out1[[1]], out2[[1]]),
          title = "Effect of state legislative service and revolving door restrictions by professionalism",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Appendix Table 17.tex",
          label = "tab:interactions2",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever Ran for House General",
          #column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          add.lines = list(c("Bandwidth", out1[[2]], out2[[2]]), 
                           c("Sample:", "Low Professionalism", "High Professionalism")),
          keep = c("won_election", "rev.cat_num", "wonXrev"), order = c(1, 3, 2),
          covariate.labels = c("Won", "Rev. Door Restriction", "Ever Won x Rev. Door"), 
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with a globally unit-standardized professionalism score for the state-chamber-year based on \\citet{bowen2014}. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. The sample is split by whether the state is a high or low professionalism legislature. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)

###
# 
# main.specification.profint <- function(x, cats) {
#   rd.data <- filter(rd.data, rev.cat %in% cats)
#   
#   rd.data$cutoff<-NULL
#   rd.data$bandwidth<-NULL
#   rd.data$kw<-NULL
#   
#   y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
#   rd.data$bandwidth<-y$bws[1]
#   rm(y)
#   
#   # gen cutoff = 0
#   rd.data$cutoff <- 0
#   # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
#   rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
#   # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
#   rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
#   
#   out <- felm(x[rd.data$kw>0] ~ won_election + wonXMDSstd + mds1_std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
#   #out <- lm(x[rd.data$kw>0] ~ won_election + won_election + wonXMDSstd + mds1_std + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates + as.factor(year) + const, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
#   bw <- round(rd.data$bandwidth[1], 2)
#   
#   return(list(out, bw))
# }
# 
# m1 <- main.specification.profint(rd.data$ever_runhouse[rd.data$rev.cat %in% c("none", "low")], c("none", "low"))
# m2 <- main.specification.profint(rd.data$ever_runhouse[rd.data$rev.cat %in% c("high", "medium")], c("high", "medium"))
# 
# 
# stargazer(list(m1[[1]], m2[[1]]),
#           title = "Effect of state legislative service and revolving door restrictions by professionalism",
#           style="apsr",
#           font.size =  "scriptsize",
#           model.numbers=T,
#           out="7tex/manuscript/tables/sourcefiles/Appendix Table 16.tex",
#           label = "tab:interactions2",
#           keep.stat=c("n","rsq"),
#           dep.var.labels  = "Ever Ran for House General",
#           #column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
#           add.lines = list(c("Bandwidth", m1[[2]], m2[[2]])),
#           keep = c("won_election", "mds1_std", "wonXMDSstd"), order = c(1, 3, 2),
#           covariate.labels = c("Won", "Professionalism", "Ever Won x Professionalism"), 
#           notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with a globally unit-standardized professionalism score for the state-chamber-year based on \\citet{bowen2014}. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. The sample is split by whether the revolving door restrictions in a state are low versus high. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
# )



m1<-main.specification.addl(rd.data$ever_ranprim_h)
m2<-main.specification.addl(rd.data$ever_winprim_h)
m3<-main.specification.addl(rd.data$ever_runhouse)
m4<-main.specification.addl(rd.data$ever_winhouse)

stargazer(list(m1[[1]], m2[[1]], m3[[1]], m4[[1]]),
          title = "Effect of state legislative service interacted with state revolving door legislation",
          style="apsr",
          font.size =  "scriptsize",
          model.numbers=T,
          out="7tex/manuscript/tables/sourcefiles/Table 5.tex",
          label = "tab:interactions_revdoor",
          keep.stat=c("n","rsq"),
          dep.var.labels  = "Ever:",
          column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
          keep = c('won_election','wonXrev'),
          covariate.labels = c("Won","Won * rev. door legis."), 
          add.lines = list(c("Bandwidth", m1[[2]], m2[[2]], m3[[2]], m4[[2]])),
          notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with an indicator of revolving door strictness from \\citet{strickland2019}. Data are available from 1990-2008 and the median year is 2002. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)







###################
# OTHER INTERACTIONS - PROFESISONALISM SEPARATE
###################


main.specification.addl <- function(x,z) {
  rd.data$cutoff<-NULL
  rd.data$bandwidth<-NULL
  rd.data$kw<-NULL
  
  y<-rdbwselect(x, rd.data$victory_marg, p=1, c = 0,  kernel = "tri", bwselect="mserd", covs = rd.data$running_terms_served + rd.data$dem + rd.data$rep + rd.data$spc_elec + rd.data$term_length + rd.data$number_candidates + rd.data$year)
  rd.data$bandwidth<-y$bws[1]
  rm(y)
  
  # gen cutoff = 0
  rd.data$cutoff <- 0
  # gen kw = 1-(abs(cutoff-victory_marg))/bandwidth
  rd.data$kw <- 1-(abs(rd.data$cutoff-rd.data$victory_marg))/rd.data$bandwidth
  # replace kw = 0 if (victory_marg>cutoff+bandwidth | victory_marg<cutoff-bandwidth) 
  rd.data$kw[rd.data$victory_marg>(rd.data$cutoff+rd.data$bandwidth) | rd.data$victory_marg<(rd.data$cutoff-rd.data$bandwidth)] <- 0
  rd.data$mainefx<-(z-mean(z,na.rm=TRUE))/sd(z,na.rm=TRUE)
  rd.data$interaction<-rd.data$won_election*rd.data$mainefx
  out <- felm(x[rd.data$kw>0] ~ won_election + interaction + mainefx + victory_marg + f_x_win + running_terms_served + dem + rep + spc_elec + term_length + number_candidates | year + state | 0 | state, data = rd.data[rd.data$kw>0,], weights = rd.data$kw[rd.data$kw>0])
  rd.data$mainefx<-NULL
  rd.data$interaction<-NULL
  return(out)
}

m1<-main.specification.addl(rd.data$ever_ranprim_h, rd.data$salary_real )
m2<-main.specification.addl(rd.data$ever_winprim_h, rd.data$salary_real)
m3<-main.specification.addl(rd.data$ever_runhouse, rd.data$salary_real)
m4<-main.specification.addl(rd.data$ever_winhouse, rd.data$salary_real)

p1<-stargazer(list(m1, m2, m3,m4),
              title = "Effect of state legislative service interacted with state legislative professionalism",
              style="apsr",
              font.size =  "scriptsize",
              model.numbers=T,
              label = "tab:interactions_separate",
              keep.stat=c("n","rsq"),
              dep.var.labels  = "Ever:",
              column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
              keep = c("interaction"),
              covariate.labels = c("Interaction"), 
              notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with a globally unit-standardized professionalism score for the state-chamber-year based on \\citet{bowen2014}. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)
m5<-main.specification.addl(rd.data$ever_ranprim_h, rd.data$slength )
m6<-main.specification.addl(rd.data$ever_winprim_h, rd.data$slength)
m7<-main.specification.addl(rd.data$ever_runhouse, rd.data$slength)
m8<-main.specification.addl(rd.data$ever_winhouse, rd.data$slength)

p2<-stargazer(list(m5, m6, m7,m8),
              title = "Effect of state legislative service interacted with state legislative professionalism",
              style="apsr",
              font.size =  "scriptsize",
              model.numbers=T,
              label = "tab:interactions_separate",
              keep.stat=c("n","rsq"),
              dep.var.labels  = "Ever:",
              column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
              keep = c("interaction"),
              covariate.labels = c("Interaction"), 
              notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with a globally unit-standardized professionalism score for the state-chamber-year based on \\citet{bowen2014}. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)



m9<-main.specification.addl(rd.data$ever_ranprim_h, rd.data$expend )
m10<-main.specification.addl(rd.data$ever_winprim_h, rd.data$expend)
m11<-main.specification.addl(rd.data$ever_runhouse, rd.data$expend)
m12<-main.specification.addl(rd.data$ever_winhouse, rd.data$expend)

p3<-stargazer(list(m9, m10, m11,m12),
              title = "Effect of state legislative service interacted with state legislative professionalism",
              style="apsr",
              font.size =  "scriptsize",
              model.numbers=T,
              label = "tab:interactions_separate",
              keep.stat=c("n","rsq"),
              dep.var.labels  = "Ever:",
              column.labels=c("Ran for House primary", "Won House primary", "Ran for House general", "Won House general"),
              keep = c("interaction"),
              covariate.labels = c("Interaction"), 
              notes = "\\parbox[t]{\\linewidth}{Note: This table reports the same model specifications as in Table \\ref{tab:main_results}, but includes an interaction with a globally unit-standardized professionalism score for the state-chamber-year based on \\citet{bowen2014}. The dependent variable is equal to one if the candidate ever runs in the election listed in the column header and is zero otherwise. The sample contains all first-time state legislative elections within the optimal bandwidth based on the \\cite{CCT} algorithm. All regressions include state and election year fixed effects, a linear term in the election margin as well as its interaction with the indicator for having won, and the full set of candidate and election controls. Estimations are triangular kernel-weighted.  Standard errors clustered by state are reported in parentheses.}"
)



source('3analysis/starpanelSOC.r')
# from https://rdrr.io/github/ChandlerLutz/starpolishr/src/R/star_tex_write.R
# from https://rdrr.io/github/ChandlerLutz/starpolishr/src/R/star_panel.R
#remotes::install_github("ChandlerLutz/starpolishr")
##stargazer panel -- same summary statistics across panels.
# star.panel.out <- star_panel(p1, p2,p3,
#                              panel.names = c("Interaction with salary", "Interaction with session length", "Interaction with expenditures"),
#                              extra.rows=)
# )
# star.panel.out
##write to a tex file and compile to check output

# Note: you have to manually format this file to perfectly regenerate the format in the manuscript. Not sure why you would want to do this though.
star_tex_write(p1, p2,p3, file = "7tex/manuscript/tables/sourcefiles/Appendix Table 13.tex", headers = TRUE)


